#include <iostream>

using std::cout;
using std::endl;

//一个类重载了函数调用运算符，并且该类创建了对象，就将该对象称为
//函数对象(这个对象可以像函数一样进行使用)
class FunctionObject
{
public:
    int operator()(int x, int y)
    {
        cout << "int operator()(int, int)" << endl;
        ++_cnt;
        return x + y;
    }

    int operator()(int x, int y, int z)
    {
        cout << "int operator()(int, int, int)" << endl;
        ++_cnt;
        return x * y * z;
    }

    int getCnt() const
    {
        return _cnt;
    }

private:
    int _cnt = 0;//函数对象携带的状态
};

int add(int x, int y)
{
    cout << "int add(int, int)" << endl;
    static int cnt = 0;
    ++cnt;
    return x + y;
}

void test()
{
    int a = 10;
    int b = 20;
    int c = 30;

    FunctionObject fo;//fo是一个对象
    /* cout <<"fo(a, b) = " << fo.operator()(a, b) << endl;//原始写法 */
    cout << "fo(a, b) = " << fo(a, b) << endl;//简写
    cout << "add(a, b) = " << add(a, b) << endl;
    /* fo.operator()(a, b, c); */
    cout << "fo(a, b, c) = " << fo(a, b, c) << endl;
    cout << fo.getCnt() << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

